Replacing Items in Collections
The MyReplaceCollectionItem function is a generic routine that you could write to replace collection items. In the implementation in Listing 3-5, the data being replaced is returned in a variable pointed to by oldData, unless the pointer isnil
. If the item does not exist, the new data is returned via the pointer instead.Listing 3-5 Replacing collection items
OSErr MyReplaceCollectionItem(void *newData, long collectSize, OSType collectType, long collectID, Collection whichCollection, Ptr *oldData, long *oldDataSize) { OSErr err; long index; /* If returning the old data, get it. If there is no old data, return a copy of the new data. */ if (oldData) { err = GetCollectionItemInfo(whichCollection, collectType, collectID, dontWantIndex, oldDataSize, dontWantAttributes); if (err) { *oldDataSize = collectSize; *oldData = NewPtrSys(*oldDataSize); if (!(err = MemError())) BlockMove(newData, *oldData, collectSize); } else { *oldData = NewPtrSys(*oldDataSize); if (!(err = MemError())) err = GetCollectionItem(whichCollection, collectType, collectID, dontWantSize, *oldData); } nrequire(err, CouldNotSetOldData); } /* Add a new collection item; otherwise, get the existing item's index and replace the old collection item. */ err = AddCollectionItem(whichCollection, collectType, collectID, collectSize, newData); if (err == collectionItemLockedErr) { err = GetCollectionItemInfo(whichCollection, collectType, collectID, &index, dontWantSize, dontWantAttributes); if (!err) err = ReplaceIndexedCollectionItem(whichCollection, index, collectSize, newData); } CouldNotSetOldData: return err; }
Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help